
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author ronaldo
 * MySql = "com.mysql.jdbc.Driver"
 * URLC= "jdbc:mysql://localhost:3306"
 * SqlServer = com.microsoft.sqlserver.jdbc.SQLServerDriver
 * URLC= "jdbc:sqlserver://localhost:1433"
 */
public class Conexion {

    public static Connection Conector;

    public Conexion() {
        try {
            Conectar("com.mysql.jdbc.Driver", "jdbc:mysql://db4free.net:3306/sistemas", "firexwin", "ronaldo");
            if (Conector.isClosed()) {
                System.out.println("Imposible conectar");
            } else {
                System.out.println("Conectado correctamente a db4Free");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Conectar(String CFName, String URLC, String User, String Pass) {
        try {
            Class.forName(CFName);
            Conector = DriverManager.getConnection(URLC, User, Pass);
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Mostrar() {
        try {
            Statement Consulta = Conector.createStatement();
            ResultSet Resultado = Consulta.executeQuery("select * from Transaccion");

            while (Resultado.next()) {
                System.out.println("\tID    \t|" + Resultado.getInt(1) + "\t|");
                System.out.println("\tDato1 \t|" + Resultado.getString(2) + "\t|");
                System.out.println("\tDato2 \t|" + Resultado.getString(3) + "\t|");
                System.out.println("\tDato3 \t|" + Resultado.getString(4) + "\t|");
                System.out.println("\tDato4 \t|" + Resultado.getString(5) + "\t|");
                System.out.println("------------------------------------------------");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Ingresar(BufferedReader BR) {
        try {
            String d1, d2, d3, d4;
            System.out.println("Digita el dato 1:");
            d1 = BR.readLine();
            System.out.println("Digita el dato 2:");
            d2 = BR.readLine();
            System.out.println("Digita el dato 3:");
            d3 = BR.readLine();
            System.out.println("Digita el dato 4:");
            d4 = BR.readLine();
            Statement Consulta = Conector.createStatement();
            Consulta.executeUpdate("INSERT INTO `personal`.`Transaccion`VALUES (NULL,'" + d1 + "','" + d2 + "','" + d3 + "','" + d4 + "');");
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void main(String a[]) {
        Conexion c = new Conexion();
        BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("1--.---Mostrar");
            System.out.println("2--.---Ingresar");
            try {
                int i = Integer.parseInt(BR.readLine().toString());
                if (i == 1) {
                    c.Mostrar();
                } else if (i == 2) {
                    c.Ingresar(BR);
                }
            } catch (IOException ex) {
                Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("\n");
        }
    }
}
